package cn.boyu.controller;


import cn.boyu.entity.AssetsAmmeter;
import cn.boyu.entity.AssetsTerminal;
import cn.boyu.entity.Gather;
import cn.boyu.query.GatherQuery;
import cn.boyu.result.AjaxResult;
import cn.boyu.service.IAssetsAmmeterService;
import cn.boyu.service.IAssetsTerminalService;
import cn.boyu.service.IGatherAmmeterService;
import cn.boyu.service.IGatherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yn
 * @since 2022-11-22
 */
@RestController
@CrossOrigin
@RequestMapping("/gather")
public class GatherController {
    @Autowired
    private IGatherService gatherService;

    @Autowired
    private IAssetsTerminalService terminalService;

    @Autowired
    private IAssetsAmmeterService ammeterService;

    @Autowired
    private IGatherAmmeterService gatherAmmeterService;

    //查询采集信息
    @GetMapping("/query")
    public AjaxResult query(GatherQuery qo){
        IPage<Gather> page = gatherService.queryPage(qo);
        return AjaxResult.success(page);
    }

    //新增采集信息
    @PostMapping("/add")
    public AjaxResult add(Gather gather){
        gatherService.saveGather(gather);
        return AjaxResult.success();
    }

    //新增采集信息中查询终端的信息
    @GetMapping("/addTerminal")
    public AjaxResult addTerminal(){
        List<AssetsTerminal> list = terminalService.listTerminal();
        return AjaxResult.success(list);
    }

    //挂接时查询的电表信息（已经挂接用户的电表）
    @GetMapping("/ammeterList")
    public AjaxResult ammeterList(Long gatherId){
        List<AssetsAmmeter> list = ammeterService.ammeterList(gatherId);
        return AjaxResult.success(list);
    }

    //挂接操作 将电表id值都挂到关联表中
    @PostMapping("/mount")
    public AjaxResult mount(Long gatherId ,String ids){
        gatherAmmeterService.mount(gatherId,ids);
        return AjaxResult.success();
    }

    //查询已挂接电表
    @GetMapping("/mountList")
    public AjaxResult mountList(Long gatherId){
        List<AssetsAmmeter> list = gatherAmmeterService.mountList(gatherId);
        return AjaxResult.success(list);
    }

    //采集信息删除
    @PostMapping("/delete")
    public AjaxResult delete(Long gatherId){
        gatherService.delele(gatherId);
        return AjaxResult.success();
    }

    //采集信息修改
    @PostMapping("/edit")
    public AjaxResult edit(Gather gather){
        gatherService.edit(gather);
        return AjaxResult.success();
    }


}

